import Vue from 'vue'
import Vuex,{Store} from 'vuex'
import {nanoid} from 'nanoid'
import axios from 'axios'

Vue.use(Vuex)

export default new Store({
  actions:{
    async getNews({commit}){
      try {
        let result = await axios.get('https://api.uomg.com/api/rand.qinghua')
        let obj = {id:nanoid(),title:result.data.content}
        commit('SAVE_NEWS',obj)
      } catch (error) {
        alert(error.message)
      }      
    }
  },
  mutations:{
    SAVE_NEWS(state,value){
      state.newsList.unshift(value)
    },
    ADD_PERSON(state,value){
      state.personList.unshift(value)
    }
  },
  state:{
    newsList:[
      {id:'asdr51',title:'《震惊，两老师竟然在办公区吃螺蛳粉》'},
      {id:'asdr52',title:'《政策出了，两个规矩定了！》'},
      {id:'asdr53',title:'《来成都，必吃的100种美食！》'}
    ],
    personList:[
      {id:'asgdftr1',name:'强哥',age:36},
      {id:'asgdftr2',name:'晶哥',age:90},
      {id:'asgdftr3',name:'向阳',age:18},
      {id:'asgdftr4',name:'曼曼',age:20}
    ]
  },
  getters:{}
})